package com.condominio.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import com.condominio.excepcion.DAOExcepcion;
import com.condominio.modelo.AreaComun;
import com.condominio.util.ConexionBD;

public class AreaComunDAO extends BaseDAO {
	public int insertAreaComun(AreaComun oAreaComun) throws DAOExcepcion {
		int i = 0;
		System.out
				.println("AreaComunDAO: insertAreaComun(AreaComun oAreaComun)");
		String query = "INSERT INTO AreaComun(TxDescripcion, TxUbicacion) "
				+ "VALUES (?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.getConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, oAreaComun.getDescripcion());
			stmt.setString(2, oAreaComun.getUbicacion());
			i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException();
			}
		} catch (SQLException e) {
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return i;
	}

	public AreaComun findAreaComun(int coArea) throws DAOExcepcion {
		System.out.println("AreaComunDAO: findAreaComun(int coArea)");
		String query = "select CoArea, TxDescripcion, TxUbicacion "
				+ "from AreaComun where CoArea = ? ";
		AreaComun oAreaComun = new AreaComun();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.getConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, coArea);
			rs = stmt.executeQuery();
			if (rs.next()) {
				oAreaComun.setCodigo(rs.getInt("CoArea"));
				oAreaComun.setDescripcion(rs.getString("TxDescripcion"));
				oAreaComun.setUbicacion(rs.getString("TxUbicacion"));
			}
		} catch (SQLException e) {
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return oAreaComun;
	}

	public void deleteAreaComun(int coArea) throws DAOExcepcion {
		System.out.println("AreaComunDAO: deleteAreaComun(int coArea)");
		String query = "DELETE FROM AreaComun where CoArea=? ";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.getConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, coArea);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void updateAreaComun(AreaComun oAreaComun) throws DAOExcepcion {
		System.out.println("AreaComunDAO: updateAreaComun(AreaComun oAreaComun)");
		String query = "update AreaComun set TxDescripcion=?, TxUbicacion = ? "
				+ "where CoArea=? ";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.getConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, oAreaComun.getDescripcion());
			stmt.setString(2, oAreaComun.getUbicacion());
			stmt.setInt(3, oAreaComun.getCodigo());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Collection<AreaComun> listAreaComun() throws DAOExcepcion {
		System.out.println("AreaComunDAO: listar()");
		String query = "select CoArea, TxDescripcion, TxUbicacion from " +
				"AreaComun";
		Collection<AreaComun> oAreasComunes = new ArrayList<AreaComun>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.getConexion();			
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				AreaComun oAreaComun = new AreaComun();
				oAreaComun.setCodigo(rs.getInt("CoArea"));
				oAreaComun.setDescripcion(rs.getString("TxDescripcion"));
				oAreaComun.setUbicacion(rs.getString("TXUbicacion"));
				oAreasComunes.add(oAreaComun);
			}
		} catch (SQLException e) {
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return oAreasComunes;
	}
}
